//
//  ADView.m
//  01CustomUITableViewCell
//
//  Created by 郝海圣 on 15/9/1.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "ADView.h"
@interface ADView()<UIScrollViewDelegate,UITextFieldDelegate>
@end
@implementation ADView{

    UITextField *_urlText;
}
-(void)dealloc{
    self.imageArray = nil;
//    [super dealloc];
}
//重写父类方法
-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //定制view
        [self customView];
    }
    return self;
}
-(void)customView{
    //创建srcollView 并添加为view的子视图
    UIScrollView *src= [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    //设置分页效果
    src.pagingEnabled = YES;
    //设置tag值
    src.tag = 20;
    //水平位置指示条
    src.showsHorizontalScrollIndicator = NO;
    src.showsHorizontalScrollIndicator = NO;
    //回弹
    src.bounces = YES;
    //设置代理
    src.delegate = self;
    [self addSubview:src];
    
//    //创建半透明的背景条
//    UIView *blackView = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-40, self.frame.size.width, 40)];
//    //设置背景颜色
//    blackView.backgroundColor = [UIColor blackColor];
//    //设置透明度
//    blackView.alpha = 0.5;
//    [self addSubview:blackView];
//    [blackView release];
    
    //创建页码显示器
    UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(150, SCREEN_HEIGHT-80, 100, 30)];
    pageControl.currentPage = 0;
    pageControl.tag = 21;
    pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];
    pageControl.pageIndicatorTintColor = [UIColor redColor];
    [self addSubview:pageControl];

    
}
//刷新数据
-(void)reloadData{
    //获得src
    UIScrollView *src = (id)[self viewWithTag:20];
   
    [self createUI];
    src.contentSize = CGSizeMake((self.imageArray.count)*SCREEN_WIDTH, SCREEN_HEIGHT );
    //获得page
    UIPageControl *page = (UIPageControl *)[self viewWithTag:21];
    page.numberOfPages = self.imageArray.count;
    
}

-(void)removeFromMy:(UIButton *)btn{
    
    self.block();
    [[NSUserDefaults standardUserDefaults]setValue:_urlText.text forKey:@"URL"];
}
#pragma mark UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //偏移量
    CGPoint point = scrollView.contentOffset;
    UIPageControl *page = (UIPageControl *)[self viewWithTag:21];
    //设置当前的page
    page.currentPage = point.x/SCREEN_WIDTH;
   
}
-(void)removeSub{
    UIPageControl *page = (id)[self viewWithTag:21];
    [page removeFromSuperview];
    UIScrollView *src = (id)[self viewWithTag:20];
    [src removeFromSuperview];
}


-(void)createUI{
    UIScrollView *src = (id)[self viewWithTag:20];
    UILabel *label  = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, SCREEN_WIDTH - 20,30)];
    label.text = @"服务器URL:";
    label.textColor = [UIColor lightGrayColor];
    [src addSubview:label];
    
    _urlText =[[UITextField alloc]initWithFrame:CGRectMake(10, 130, SCREEN_WIDTH-100, 40)];
    _urlText.placeholder=@"请输入您的服务器地址";
    _urlText.layer.cornerRadius =10;
    _urlText.layer.borderColor =[UIColor blackColor].CGColor;
    _urlText.backgroundColor =[UIColor whiteColor];
    _urlText.clearButtonMode = UITextFieldViewModeAlways;
    _urlText.tag = 1;
    _urlText.delegate=self;
    [src addSubview:_urlText];
    
    UILabel *labelLine  = [[UILabel alloc]initWithFrame:CGRectMake(10, 171, SCREEN_WIDTH - 20,2)];
    labelLine.textColor = [UIColor orangeColor];
    labelLine.layer.borderWidth = 1;
    labelLine.layer.borderColor = [[UIColor orangeColor]CGColor];
    [src addSubview:labelLine];
    
    
//      登陆按钮
    UIButton * logbutton =[[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, 180, 100, 40)];
    
    //  [logbutton setImage:[UIImage imageNamed:@"login_btn_login"] forState:UIControlStateNormal];
    [logbutton setTitle:@"确定" forState:UIControlStateNormal];
    logbutton.tag = 9999;
    logbutton.backgroundColor = [UIColor orangeColor];
    [logbutton addTarget:self action:@selector(removeFromMy:) forControlEvents:UIControlEventTouchUpInside];
    [src addSubview:logbutton];
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
